package rgbos.forum.tests;

import java.util.Calendar;
import java.util.Vector;
import rgbos.forum.domain.Post;
import rgbos.forum.domain.Topic;
import rgbos.forum.domain.User;
import rgbos.forum.persistance.Persistance;
import junit.framework.TestCase;

public class PersistanceTest extends TestCase {

	private Vector<Post> postVec = new Vector<Post>();
	private Topic topic1_;
	private Persistance pers_ = new Persistance("SQL");//XML OR SQL?
	int topicNumber;
	
	protected void setUp() throws Exception {
		super.setUp();
		pers_.connect();
		//init the DB
//		pers_.createNewTables();
		//topic1_= new Topic(16, "topic16", "author1",Calendar.getInstance().getTime(),new Vector<Integer>(), 0, 0);
		//topicNumber=pers_.saveNewTopic(topic1_);
		//Creating Posts :
//		for (int i=0; i < 5; i++){
//			postVec.add(new Post(i,"title"+i,String.valueOf(i),Calendar.getInstance().getTime(),"body"+i));
//			//topic1_.getPostVector().add(i);
//
//		}
	}
	/**
	 * check if the number of posts of specific topic was increment by 1
	 */
//	public void testPostCreate(){
//
//		int postCount = pers_.getTopicById(1).getPostCount();
//		pers_.saveNewPost(postVec.get(0),1);
//		assertEquals(postCount+1, pers_.getTopicById(1).getPostCount());
//	}

//	public void testPostDelete(){
//		int postCountBefore = pers_.getTopicById(16).getPostCount();
//		int postId = pers_.saveNewPost(postVec.get(0),16);
//		assertTrue(pers_.deletePost(postId));
//		assertEquals(postCountBefore, pers_.getTopicById(16).getPostCount());
//		assertFalse(pers_.deletePost(postId)); //all ready deleted
//		assertFalse(pers_.deletePost(3)); //not exist
//	}

//	public void testPostModified() throws ParseException{
//		int postId = pers_.saveNewPost(postVec.get(0),16);
//		assertTrue(pers_.modifyPost(new Post(postId,"title"+3,"3",Calendar.getInstance().getTime(),"MODIFIED")));
//		//there isn't such post in the system:
//		assertFalse(pers_.modifyPost(new Post(55555,"title"+3,"3",Calendar.getInstance().getTime(),"MODIFIED")));
//	}

//	public void testPostModifyBody(){
//		int postId = pers_.saveNewPost(postVec.get(0),16);
//		assertTrue(pers_.modifyPostBody(postId,"newTitle","body modified"));
//	}


//	/**
//	 *  TOPICS TESTS :
//	 */

//	public void testTopicCreate(){
//		int numOfTopics = pers_.getTopics().size();
//		pers_.saveNewTopic(topic1_);
//		assertEquals(numOfTopics+1, pers_.getTopics().size());
//	}

//	public void testTopicDelete(){
//		int topicId =  pers_.saveNewTopic(topic1_);
//		assertTrue(pers_.deleteTopic(topicId));
//		assertFalse(pers_.deleteTopic(topicId)); //all ready deleted
//		assertFalse(pers_.deleteTopic(505050505)); // not exist
//		int topicId2 = pers_.saveNewTopic(topic1_);
//		assertTrue(pers_.deleteTopic(topicId2));
//	}

//	public void testTopicModify() throws ParseException{
//		int topicId =  pers_.saveNewTopic(topic1_);
//		pers_.modifyTopic(new Topic(topicId, "topic1", "author1",Calendar.getInstance().getTime(),new Vector<Integer>(), 0, 0));
//
//	}

//	public void testTopicById(){
//		int topicId = pers_.saveNewTopic(topic1_);
//		Topic topic = pers_.getTopicById(topicId);
//		System.out.println(topic.getTitle_());
//		System.out.println(topic.getAuthorName());
//		System.out.println(topic.getCreationDate_());
//	}

//	/**
//	 * USERS TESTS:
//	 */

//	public void testUserCreateAndLogin(){
//		assertTrue(pers_.registerNewUser(new User("11", "6512bd43d9caa6e02c990b0a82652dca", "keinang@bgu.ac.il",1)));
//		assertFalse(pers_.registerNewUser(new User("gilad", "1", "keinang@bgu.ac.il",0)));
//		assertTrue(pers_.registerNewUser(new User("shaul", "A1234567890", "mansours@bgu.ac.il",0)));
//		assertTrue(pers_.logIn("gilad", "password"));//OK
//		assertFalse(pers_.logIn("shaul", "wrongPass"));//Wrong Password
//	}

	public void testUserDelete(){
//		assertTrue(pers_.registerNewUser(new User("gilad2", "password", "keinang@bgu.ac.il",0)));
		assertTrue(pers_.deleteUser("what''s"));
//		assertFalse(pers_.deleteUser("gilad2")); //all ready deleted
//		assertFalse(pers_.deleteUser("shaul")); //not exist
	}

//	public void testUserModify(){
//		assertTrue(pers_.registerNewUser(new User("gilad33", "password", "keinang@bgu.ac.il",0)));
//		assertTrue(pers_.modifyUserDetails(new User("gilad33", "password", "MODIFIED",0)));
//		assertFalse(pers_.modifyUserDetails(new User("shual", "password", "MODIFIED",0)));
//	}

//	public void testSetAdmin(){
//		assertTrue(pers_.registerNewUser(new User("gilad4", "password", "keinang@bgu.ac.il",0)));
//		assertTrue(pers_.setAdmin("gilad4"));
//		assertTrue(pers_.checkAdmin("gilad4"));
//		assertFalse(pers_.checkAdmin("NOT A USER"));
//		assertTrue(pers_.deleteUser("gilad4"));
//	}

//	public void testChecks(){
//		assertTrue(pers_.registerNewUser(new User("gilad4", "password", "keinang@bgu.ac.il",0)));
//		assertTrue(pers_.setAdmin("gilad4"));
//		assertTrue(pers_.checkAdmin("gilad4"));
//		assertFalse(pers_.checkAdmin("NOT A USER"));
//		assertTrue(pers_.deleteUser("gilad4"));
//		assertTrue(pers_.registerNewUser(new User("gilad5", "password", "keinang@bgu.ac.il",0)));
//		assertTrue(pers_.setModerator("gilad5"));
//		assertTrue(pers_.checkModerator("gilad5"));
//		assertFalse(pers_.checkModerator("NOT A USER"));
//		assertTrue(pers_.deleteUser("gilad5"));
//	}

//	public void testCheckExistUser(){
//		assertTrue(pers_.registerNewUser(new User("gilad4", "password", "keinang@bgu.ac.il",0)));
//		assertTrue(pers_.checkExistUser("gilad4"));
//		assertFalse(pers_.checkExistUser("NOT A USER"));
//		assertTrue(pers_.deleteUser("gilad4"));
//	}

	public void testSetModerator(){
//		assertTrue(pers_.registerNewUser(new User("gilad4", "password", "keinang@bgu.ac.il",0)));
//		assertTrue(pers_.setAdmin("benny"));
//		assertFalse(pers_.setModerator("shaul"));
//		assertTrue(pers_.registerNewUser(new User("shaul", "password", "keinang@bgu.ac.il",0)));
//		assertTrue(pers_.setModerator("shaul"));
//		assertTrue(pers_.checkModerator("shaul"));
//		assertFalse(pers_.checkModerator("not a moderator"));
//		assertTrue(pers_.deleteUser("gilad4"));
	}

//	public void testSetModerator(){
//		assertTrue(pers_.setModerator("11"));
//	}

//	public void testGetEmailByName(){
//		assertTrue(pers_.registerNewUser(new User("gilad4", "password", "keinang@bgu.ac.il",0)));
//		assertEquals(pers_.getEmailByName("gilad4"),"keinang@bgu.ac.il");
//		pers_.deleteUser("gilad4");
//	}
//	public void testResetOnlineUsers(){
//		Vector<User> users = pers_.getOnlineUsers();
//		for (User user : users) {
//			this.pers_.setOnlineBit(user.getUsername(), 0); //offline
//		}
//	}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GETTERS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	public void testGetPosts(){
		Vector<Post> posts = pers_.getPosts();
		System.out.println("~~~~~~~~~~  Posts ~~~~~~~~~~~~~~~~");
		System.out.println("Number of Posts = " + pers_.getPosts().size());
		for (Post post :posts){
			System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
			System.out.println("id = " +post.getId_());
			System.out.println("title = " +post.getTitle_());
			System.out.println("body = " +post.getBody_());
			System.out.println("Creation Date= "+post.getCreationDate_().toGMTString());
			System.out.println("author name = "+post.getAuthorName());
		}
		System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
	}
	public void testGetTopics(){
		Vector<Topic> topics = pers_.getTopics();
		System.out.println("~~~~~~~~~~  Topics ~~~~~~~~~~~~~~~~");
		System.out.println("Number of Topics = " + pers_.getTopics().size());
		for (Topic t : topics){
			System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
			System.out.println("id = "+t.getId_());
			System.out.println("title = "+t.getTitle_());
			System.out.println("author = "+t.getAuthorName());
			System.out.println("date = "+t.getCreationDate_().toString());
			System.out.println("post count = "+t.getPostCount());
			System.out.println("view count = "+t.getViewCount());
			Vector<Integer> posts = t.getPostVector();
			for (int i :posts){
				System.out.println("post # = "+i);
			}
			System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
		}
	}
	public void testGetUsers(){
		Vector<User> users = pers_.getUsers();
		System.out.println("~~~~~~~~~~  USERS ~~~~~~~~~~~~~~~~");
		System.out.println("Number of users = " + users.size());
		for (User user :users){
			System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
			System.out.println("userName ="+ user.getUsername());
			System.out.println("password ="+ user.getPassword());
			System.out.println("Email ="+ user.getEmail());
			System.out.println("Permission Group ="+ user.getPermmisionGroup());
		}
		System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
	}


	public void testGetOnlineUsers(){
		Vector<User> users = pers_.getOnlineUsers();
		System.out.println("~~~~~~~~~~  Online USERS ~~~~~~~~~~~~~~~~");
		System.out.println("Number of users = " + users.size());
		for (User user :users){
			System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
			System.out.println("userName ="+ user.getUsername());
			System.out.println("password ="+ user.getPassword());
			System.out.println("Email ="+ user.getEmail());
			System.out.println("Permission Group ="+ user.getPermmisionGroup());
		}
		System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
	}
//	public void testUsersHoursStats(){
//		pers_.incHour(1);
//		pers_.incHour(0);//will not work
//		pers_.incHour(24);
//		pers_.incHour(25);//will not work
////		int[] allHours = pers_.getHourStats();
////		for (int i=0;i<allHours.length;i++){
////			System.out.println(allHours[i]);
////		}
//	}
//	public void testGetUserStat(){
//		int[] arr=pers_.getHourStats();
//		for (int i = 0; i < arr.length; i++) {
//			System.out.println("hour["+i+"] ="+ arr[i]);
//		}
//	}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Extras TABLES~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//	public void testAddCulomn(){
//		pers_.addCulumn("isOnline","INTEGER");
//	}

//	public void testCreateUsersHours(){
//		pers_.createUsersHours();
//	}

//	public void testSetAdmin(){
//		pers_.registerNewUser(new User("33", "6512bd43d9caa6e02c990b0a82652dca", "keinang@bgu.ac.il",2));
//	}
//~~~~~~~~~~~~~ ACCEPTANCE TESTS ~~~~~~~~~~~~~~~~~~~~~~~~~~~
	
//	public void testPostDelete(){
//		topic1_ = new Topic(1, "topic1", "author1",Calendar.getInstance().getTime(),new Vector<Integer>(), 0, 0);
//		Post pos1 = new Post(0,"title1", "author1",Calendar.getInstance().getTime(),"body");
//		Post pos2 = new Post(0,"title2", "author2",Calendar.getInstance().getTime(),"body2");
//		//adding new topic:
//		int topicId = pers_.saveNewTopic(topic1_);
//		//adding new post:
//		int  pos1Index = pers_.saveNewPost(pos1, topicId);
//		int  pos2Index = pers_.saveNewPost(pos2, topicId);
//		//check  for  posts presence:
//		assertEquals( 2,pers_.getTopicById(topicId).getPostCount());
//		//delete this post:
//		assertTrue(pers_.deletePost(pos1Index));
//		//delete again this post (The post doesn't exist)
//		assertFalse(pers_.deletePost(pos1Index)); //all ready deleted
//		// delete an non existing post :
//		assertFalse(pers_.deletePost(-1));  //post id is always positive
//		//checking for data consistency
//		assertEquals( 1,pers_.getTopicById(topicId).getPostCount());
//	}

//	public void testUsersHoursStats(){
//		int[] allHours = pers_.getHourStats();
//		for (int i=0;i<allHours.length;i++){
//			System.out.println(allHours[i]);
//		}
//		//register new user:
//		assertTrue(pers_.registerNewUser(new User("giladNEW14", "password",
//		"keinang@bgu.ac.il",0)));
//		//currently there is 0 users loged in at 1
//		int[] hours = pers_.getHourStats();
//		assertEquals(hours[2],0);
//		//new user was login
//		assertTrue(pers_.logIn("giladNEW14","password"));
//		//inc at hour 1:
//		assertTrue(pers_.incHour(3));
//		int[] hours2 = pers_.getHourStats();
//		System.out.println(hours2[2]);
//		assertEquals(hours2[2],1);
//		System.out.println("3333");
//		//inc in hour not good:
//		assertFalse(pers_.incHour(0));//will not work
//		//inc in hour good:
//		System.out.println("1111");
//		assertTrue(pers_.incHour(24));
//		//inc in hour not good:
//		System.out.println("2222");
//		assertFalse(pers_.incHour(25));//will not work
//		//getting all the total user's by hour
//		int[] allHours1 = pers_.getHourStats();
//		for (int i=0;i<allHours1.length;i++){
//			System.out.println(allHours1[i]);
//		}
//	}
//
//	public void testOnlineBit(){
//		//register new user
//		assertTrue(pers_.registerNewUser(new User("giladNEW1", "ZZZPASS", "lomeshanezzz",0)));
//		//the user is currently offline
//		assertEquals(pers_.getOnlineBit("giladNEW1"),0);
//		//login
//		assertTrue(pers_.logIn("giladNEW1", "ZZZPASS"));
//		//the user is currently online
//		assertEquals(pers_.getOnlineBit("giladNEW1"),1);
//		//can't set online with number 2
//		assertFalse(pers_.setOnlineBit("giladNEW1", 2));
//		//setting online bit to user ZZZ
//		assertTrue(pers_.setOnlineBit("giladNEW1", 1));
//		//can't set online to not exist user
//		assertFalse(pers_.setOnlineBit("not Exist User", 1));
//		//setting online bit to user ZZZ
//		assertTrue(pers_.setOnlineBit("giladNEW1", 1));
//	}
//	public void testEmailModify(){
//		//register new user
//		assertTrue(pers_.registerNewUser(new User("modify", "modify", "before@before",0)));
//		//login
//		assertTrue(pers_.logIn("modify", "modify"));
//		//modify email
//		User newUser = new User("modify","modify","after@after",0);
//		//modifying the email
//		assertTrue(pers_.modifyUserDetails(newUser));
//		//check the new email
//		assertEquals(pers_.getEmailByName("modify"),"after@after");
//		//not exist user
//		assertEquals(pers_.getEmailByName("NOT EXIST USER"),"");
//	}
	
//	public void testPostDelete(){
//		topic1_ = new Topic(1, "topic1", "author1",Calendar.getInstance().getTime(),new Vector<Integer>(), 0, 0);
//		Post pos1 = new Post(0,"title1", "author1",Calendar.getInstance().getTime(),"body");
//		Post pos2 = new Post(0,"title2", "author2",Calendar.getInstance().getTime(),"body2");
//		//adding new topic:
//		int topicId = pers_.saveNewTopic(topic1_);
//		//adding new post:
//		int  pos1Index = pers_.saveNewPost(pos1, topicId);
//		int  pos2Index = pers_.saveNewPost(pos2, topicId);
//		//modify post details :
//		assertTrue (pers_.modifyPostBody(pos1Index,"newTitle","newBody"));
//		//check for modify :
//		assertEquals("newBody",pers_.getPostById(pos1Index).getBody_());
//		assertEquals("newTitle",pers_.getPostById(pos1Index).getTitle_());
//		//modify a non existing post
//		assertFalse (pers_.modifyPostBody(-1,"newTitle","newBody"));
//	}
	
	
}
